package com.sise.foodplatform.controller;

import com.alibaba.fastjson.JSON;
import com.sise.foodplatform.pojo.User;
import com.sise.foodplatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(@RequestBody User user){
        String flag = "error";
        User u = userService.getUserByMessage(user.getUsername(), user.getPassword());
        HashMap<String,Object> res = new HashMap<>();
        if (u!=null){
            flag="ok";
        }
        res.put("flag",flag);
        res.put("user",u);
        String res_json = JSON.toJSONString(res);//转换成JSON格式
        return res_json;
    }

    @RequestMapping("/register")
    public String Register(@RequestBody User user){
        user.setRole("用户");
        user.setState(true);
        int i = userService.addUser(user);
        return i>0?"success":"error";
    }
}
